﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using MP.LayerHelper.MP_OBS.BLL;
using MP.LayerHelper.MP_OBS.DAL.EntityClasses;
using MP.LayerHelper.MP_OBS.DAL.HelperClasses;
using MP.Core.Authentication;

public partial class Member_Manual : MP.Core.Authentication.Page.AuthenticationBase
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        MembershipUserCollection users = Membership.FindUsersByName(txtUser.Text.Trim());
        if (users.Count == 0)
        {
            Membership.CreateUser(txtUser.Text.Trim(), txtPassword.Text.Trim());
        }

        if (!Roles.RoleExists(txtRole.Text.Trim()))
        {
            Roles.CreateRole(txtRole.Text.Trim());
        }

        AspnetRolesEntity role = AspnetRolesManager.CreateInstant().GetRole(txtRole.Text.Trim(), Membership.ApplicationName);

        if (!Roles.IsUserInRole(txtUser.Text.Trim(), txtRole.Text.Trim()))
        {
            Roles.AddUserToRole(txtUser.Text.Trim(), txtRole.Text.Trim());
        }

        EntityCollection<MbModulesEntity> modules = MbModulesManager.CreateInstant().SelectByParentCodeLST("ADMIN");
        if (modules != null && modules.Count > 0 && role != null)
        {
            for (int i = 0; i < modules.Count; i++)
            {
                MPRoleProvider.CreateInstant().UpdateRoleModule(modules[i].ModCode, role.RoleId, Guid.Empty, true, true, true, true, true, true, true, true, true);
            }
        }
    }
}
